package com.h3c.nem.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;

import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Getter
public enum IdTypeEnum {

    ID_CARD("01", "1", "身份证"),
    HK_MC_CARD("02", "7", "港澳(永久性)居民身份证"),
    TAI_WAN_CARD("03", "4", "台湾居民身份证"),
    CN_OFFICER_CARD("CJ", "5", "中国军官证"),
    PASSPORT("KB", "2", "外国护照"),
    HOME_RETURN_PERMIT("KE", "3", "港澳居民回乡证");

//    6 其他
//    CN_DISCHARGE_CARD("CT",  "中国退伍证"),
//    CN_DRIVING_LICENSE("KD", "中国驾驶证"),
//    FOREIGN_RESIDENCE_PERMIT("KF", "外国人居留证"),
//    FOREIGN_TEMP_RESIDENCE_PERMIT("KG", "外国人临时居留证");

    IdTypeEnum(String sapValue,String beiSenValue,String name) {
        this.sapValue = sapValue;
        this.beiSenValue = beiSenValue;
        this.name = name;
    }

    @EnumValue
    private final String beiSenValue;
    private final String sapValue;
    @JsonValue
    private final String name;



    /**
     * code 作为 key，封装为 Map
     */
    private static final Map<String, IdTypeEnum> BEISEN_VALUE_MAP = Stream
            .of(IdTypeEnum.values())
            .collect(Collectors.toMap(IdTypeEnum::getBeiSenValue, Function.identity()));


    public static IdTypeEnum getEnumByValue(String beiSenValue) {
        for (IdTypeEnum idTypeEnum : IdTypeEnum.values()) {
            if (idTypeEnum.beiSenValue.equals(beiSenValue)) {
                return idTypeEnum;
            }
        }
        return null;
    }

    /**
     * 静态工厂反序列化
     *
     * @param key code
     * @return 评论的类型枚举
     */
    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static IdTypeEnum valueOfKey(String key) {
        return Optional.ofNullable(BEISEN_VALUE_MAP.get(key))
                .orElseThrow(() -> new IllegalArgumentException(String.valueOf(key)));
    }
}
